home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / boards / retina / rblanker.lha / RBlankers / RShapes.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  23KB  |  839 lines

  1. /*
  2.  * RShapes
  3.  *
  4.  * A screen blanker for the Retina graphics card.
  5.  * By Michael Heinz, 29 March, 1994
  6.  *
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <exec/ports.h>
  11. #include <exec/execbase.h>
  12. #include <graphics/displayinfo.h>
  13. #include <intuition/intuitionbase.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/commodities.h>
  16. #include <libraries/gadtools.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/dostags.h>
  19. #include <utility/tagitem.h>
  20.  
  21. #include <clib/alib_protos.h>
  22. #include <clib/commodities_protos.h>
  23. #include <clib/dos_protos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/gadtools_protos.h>
  26. #include <clib/graphics_protos.h>
  27. #include <clib/intuition_protos.h>
  28. #include <clib/macros.h>
  29. #include <clib/retina_protos.h>
  30. #include <retina/retina.h>
  31.  
  32. #include <string.h>
  33. #include <math.h>
  34. #include <stdlib.h>
  35.  
  36. #include <pragmas/commodities_pragmas.h>
  37. #include <pragmas/dos_pragmas.h>
  38. #include <pragmas/exec_pragmas.h>
  39. #include <pragmas/gadtools_pragmas.h>
  40. #include <pragmas/graphics_pragmas.h>
  41. #include <pragmas/intuition_pragmas.h>
  42. #include <pragmas/retina_pragmas.h>
  43. #include "blanker.h"
  44.  
  45. UBYTE *VersionString = "$VER: RShapes 2.0 (By Michael Heinz)";
  46.  
  47. unsigned char color_table[] =
  48. {
  49.     0, 0, 0, 0, 0, 70, 0, 0, 76, 0, 0, 82, 0, 0, 88, 0, 0, 94,
  50.     0, 0, 100, 0, 0, 106, 0, 0, 112, 0, 0, 118, 0, 0, 124, 0, 0, 130,
  51.     0, 0, 136, 0, 0, 142, 0, 0, 148, 0, 0, 154, 0, 0, 160, 0, 0, 166,
  52.     0, 0, 172, 0, 0, 178, 0, 0, 184, 0, 0, 190, 0, 0, 196, 0, 0, 202,
  53.     0, 0, 208, 0, 0, 214, 0, 0, 220, 0, 0, 226, 0, 0, 232, 0, 0, 238,
  54.     0, 0, 244, 0, 0, 250, 0, 0, 255, 4, 0, 255, 8, 0, 255, 12, 0, 255,
  55.     16, 0, 255, 20, 0, 255, 24, 0, 255, 28, 0, 255, 32, 0, 255, 36, 0, 255,
  56.     40, 0, 255, 44, 0, 255, 48, 0, 255, 52, 0, 255, 56, 0, 255, 60, 0, 255,
  57.     64, 0, 255, 68, 0, 255, 72, 0, 255, 76, 0, 255, 80, 0, 255, 84, 0, 255,
  58.     88, 0, 255, 92, 0, 255, 96, 0, 255, 100, 0, 255, 104, 0, 255, 108, 0, 255,
  59.     112, 0, 255, 116, 0, 255, 120, 0, 255, 124, 0, 255, 128, 0, 255, 132, 0, 255,
  60.     136, 0, 255, 140, 0, 255, 144, 0, 255, 148, 0, 255, 152, 0, 255, 156, 0, 255,
  61.     160, 0, 255, 164, 0, 255, 168, 0, 255, 172, 0, 255, 176, 0, 255, 180, 0, 255,
  62.     184, 0, 255, 188, 0, 255, 192, 0, 255, 196, 0, 255, 200, 0, 255, 204, 0, 255,
  63.     208, 0, 255, 212, 0, 255, 216, 0, 255, 220, 0, 255, 224, 0, 255, 228, 0, 255,
  64.     232, 0, 255, 236, 0, 255, 240, 0, 255, 244, 0, 255, 248, 0, 255, 252, 0, 255,
  65.     255, 0, 252, 255, 0, 248, 255, 0, 244, 255, 0, 240, 255, 0, 236, 255, 0, 232,
  66.     255, 0, 228, 255, 0, 224, 255, 0, 220, 255, 0, 216, 255, 0, 212, 255, 0, 208,
  67.     255, 0, 204, 255, 0, 200, 255, 0, 196, 255, 0, 192, 255, 0, 188, 255, 0, 184,
  68.     255, 0, 180, 255, 0, 176, 255, 0, 172, 255, 0, 168, 255, 0, 164, 255, 0, 160,
  69.     255, 0, 156, 255, 0, 152, 255, 0, 148, 255, 0, 144, 255, 0, 140, 255, 0, 136,
  70.     255, 0, 132, 255, 0, 128, 255, 0, 124, 255, 0, 120, 255, 0, 116, 255, 0, 112,
  71.     255, 0, 108, 255, 0, 104, 255, 0, 100, 255, 0, 96, 255, 0, 92, 255, 0, 88,
  72.     255, 0, 84, 255, 0, 80, 255, 0, 76, 255, 0, 72, 255, 0, 68, 255, 0, 64, 255, 0, 60,
  73.     255, 0, 56, 255, 0, 52, 255, 0, 48, 255, 0, 44, 255, 0, 40, 255, 0, 36, 255, 0, 32,
  74.     255, 0, 28, 255, 0, 24, 255, 0, 20, 255, 0, 16, 255, 0, 12, 255, 0, 8, 255, 0, 4,
  75.     255, 0, 0, 255, 4, 0, 255, 8, 0, 255, 12, 0, 255, 16, 0, 255, 20, 0, 255, 24, 0,
  76.     255, 28, 0, 255, 32, 0, 255, 36, 0, 255, 40, 0, 255, 44, 0, 255, 48, 0, 255, 52, 0,
  77.     255, 56, 0, 255, 60, 0, 255, 64, 0, 255, 68, 0, 255, 72, 0, 255, 76, 0,
  78.     255, 80, 0, 255, 84, 0, 255, 88, 0, 255, 92, 0, 255, 96, 0, 255, 100, 0,
  79.     255, 104, 0, 255, 108, 0, 255, 112, 0, 255, 116, 0, 255, 120, 0, 255, 124, 0,
  80.     255, 128, 0, 255, 132, 0, 255, 136, 0, 255, 140, 0, 255, 144, 0, 255, 148, 0,
  81.     255, 152, 0, 255, 156, 0, 255, 160, 0, 255, 164, 0, 255, 168, 0, 255, 172, 0, 255, 176, 0,
  82.     255, 180, 0, 255, 184, 0, 255, 188, 0, 255, 192, 0, 255, 196, 0, 255, 200, 0,
  83.     255, 204, 0, 255, 208, 0, 255, 212, 0, 255, 216, 0, 255, 220, 0, 255, 224, 0,
  84.     255, 228, 0, 255, 232, 0, 255, 236, 0, 255, 240, 0, 255, 244, 0, 255, 248, 0,
  85.     255, 252, 0, 255, 255, 0, 255, 255, 8, 255, 255, 16, 255, 255, 24, 255, 255, 32,
  86.     255, 255, 40, 255, 255, 48, 255, 255, 56, 255, 255, 64, 255, 255, 72, 255, 255, 80,
  87.     255, 255, 88, 255, 255, 96, 255, 255, 104, 255, 255, 112, 255, 255, 120, 255, 255, 128,
  88.     255, 255, 136, 255, 255, 144, 255, 255, 152, 255, 255, 160, 255, 255, 168, 255, 255, 176,
  89.     255, 255, 184, 255, 255, 192, 255, 255, 200, 255, 255, 208, 255, 255, 216, 255, 255, 224,
  90.     255, 255, 232, 255, 255, 240, 255, 255, 248, 255, 255, 255,
  91. };
  92.  
  93. /*
  94.  * A handy request structure for reporting that we've up and died.
  95.  */
  96. struct EasyStruct quitreq =
  97. {
  98.     sizeof(struct EasyStruct),
  99.     0,
  100.     "RCurves",
  101.     "RCurves has suffered an untimely demise\ndue to: %s",
  102.     "OK|OK"
  103. };
  104.  
  105. /*
  106.  * Definitions for our Commodity
  107.  */
  108. struct NewBroker NewBroker =
  109. {NB_VERSION, "RCurves ", NULL,
  110.  "Silly Rabbit, Qix are for Qids!", NBU_NOTIFY | NBU_UNIQUE, COF_SHOW_HIDE,
  111.  0, NULL, 0};
  112.  
  113. #define MAX_SPEED     20L
  114. #define MAX_LINES     300L
  115. #define LINE_BUF      512L
  116.  
  117. #define MIN_LINES      10L
  118. #define DEF_LINES     100L
  119. #define DEF_SPEED     10L
  120.  
  121. #define MIN_VERTEX    3L
  122. #define MAX_VERTEX    36L
  123. #define DEF_VERTEX    5L
  124.  
  125. #define DELTA_V          10.0
  126.  
  127. LONG NumLines, Vertices, Speed;
  128.  
  129. typedef struct {
  130.     unsigned char color;
  131.     int cur_point;
  132.     int last_point;
  133.     int max_points;
  134.     int tic, speed;
  135.     double verts, av, bv, cv, dv;
  136.     double point_list[LINE_BUF][4];
  137. } PList;
  138.  
  139. PList plist;
  140.  
  141. /*
  142.  * Definitions for our configuration window
  143.  */
  144.  
  145. struct NewWindow NewBlankerWindow =
  146. {
  147.     80, 16, 0, 0, 0, 1,
  148.     IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_GADGETDOWN |
  149.     IDCMP_GADGETUP | IDCMP_VANILLAKEY | SLIDERIDCMP | LISTVIEWIDCMP,
  150.     WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | SIMPLE_REFRESH,
  151.     NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
  152.     WBENCHSCREEN
  153. };
  154.  
  155. struct Window *BlankerWindow;
  156.  
  157. #define GID_HIDE          1
  158. #define GID_BLANK         2
  159. #define GID_QUIT          3
  160. #define GID_TIMEOUT       4
  161. #define GID_CLIENT        5
  162. #define GID_LINES          6
  163. #define GID_SPEED         7
  164. #define GID_VERTEX      8
  165.  
  166. #define NUM_GADS         8
  167.  
  168. struct VisualInfo *BlankerVisualInfo;
  169. struct Gadget *BlankerGadgets;
  170. struct TextAttr BlankerAttr =
  171. {"topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT};
  172.  
  173. struct NewGadget NewBlankerGadgets[NUM_GADS] =
  174. {16, 101, 48, 12, "_Hide", &BlankerAttr, GID_HIDE, PLACETEXT_IN, NULL, NULL,
  175.  80, 101, 48, 12, "_Blank", &BlankerAttr, GID_BLANK, PLACETEXT_IN, NULL, NULL,
  176.  144, 101, 48, 12, "_Quit", &BlankerAttr, GID_QUIT, PLACETEXT_IN, NULL, NULL,
  177.  136, 5, 66, 12, "Timeout", &BlankerAttr, GID_TIMEOUT, PLACETEXT_LEFT, NULL, NULL,
  178.  136, 21, 66, 12, "Client Timeout", &BlankerAttr, GID_CLIENT, PLACETEXT_LEFT, NULL, NULL,
  179.  136, 37, 66, 12, "Vertices ", &BlankerAttr, GID_VERTEX, PLACETEXT_LEFT, NULL, NULL,
  180.  136, 53, 66, 12, "Speed    ", &BlankerAttr, GID_SPEED, PLACETEXT_LEFT, NULL, NULL,
  181.  136, 69, 66, 12, "Lines    ", &BlankerAttr, GID_LINES, PLACETEXT_LEFT, NULL, NULL,
  182. };
  183.  
  184. UBYTE BlankerGadgetKinds[NUM_GADS] =
  185. {
  186.     BUTTON_KIND, BUTTON_KIND, BUTTON_KIND, INTEGER_KIND, INTEGER_KIND, SLIDER_KIND,
  187.     SLIDER_KIND, SLIDER_KIND
  188. };
  189.  
  190. struct TagItem ButtonGadgetTags[] =
  191. {GT_Underscore, (ULONG) '_', TAG_DONE, 0L};
  192.  
  193. struct TagItem TimeGadgetTags[] =
  194. {GTIN_Number, 0L, GTIN_MaxChars, 4L, TAG_DONE, 0L};
  195.  
  196. struct TagItem ClientGadgetTags[] =
  197. {GTIN_Number, 0L, GTIN_MaxChars, 2L, TAG_DONE, 0L};
  198.  
  199. struct TagItem SpeedGadgetTags[] =
  200. {GTSL_Level, 0L, GTSL_Min, 1L, GTSL_Max, MAX_SPEED, GTSL_LevelFormat, 0L,
  201.  GTSL_LevelPlace, PLACETEXT_LEFT, GTSL_MaxLevelLen, 2L, GA_RELVERIFY, TRUE,
  202.  TAG_DONE, 0L};
  203.  
  204. struct TagItem VertexGadgetTags[] =
  205. {GTSL_Level, 0L, GTSL_Min, MIN_VERTEX, GTSL_Max, MAX_VERTEX, GTSL_LevelFormat, 0L,
  206.  GTSL_LevelPlace, PLACETEXT_LEFT, GTSL_MaxLevelLen, 2L, GA_RELVERIFY, TRUE,
  207.  TAG_DONE, 0L};
  208.  
  209. struct TagItem LinesGadgetTags[] =
  210. {GTSL_Level, 0L, GTSL_Min, MIN_LINES, GTSL_Max, MAX_LINES, GTSL_LevelFormat, 0L,
  211.  GTSL_LevelPlace, PLACETEXT_LEFT, GTSL_MaxLevelLen, 3L, GA_RELVERIFY, TRUE,
  212.  TAG_DONE, 0L};
  213.  
  214. struct TagItem *BlankerGadgetTagLists[NUM_GADS] =
  215. {
  216.     &ButtonGadgetTags[0],
  217.     &ButtonGadgetTags[0],
  218.     &ButtonGadgetTags[0],
  219.     &TimeGadgetTags[0],
  220.     &ClientGadgetTags[0],
  221.     &VertexGadgetTags[0],
  222.     &SpeedGadgetTags[0],
  223.     &LinesGadgetTags[0],
  224. };
  225.  
  226. void
  227. CloseBlankerWindow(void)
  228. {
  229.     if (BlankerWindow) {
  230.         /*
  231.          * We save the current position of the window
  232.          * so it will re-open in the same place, later.
  233.          */
  234.         NewBlankerWindow.LeftEdge = BlankerWindow->LeftEdge;
  235.         NewBlankerWindow.TopEdge = BlankerWindow->TopEdge;
  236.  
  237.         RemTool(BlankerGadgets);
  238.         RemTool(BlankerVisualInfo);
  239.         RemTool(BlankerWindow);
  240.         BlankerWindow = NULL;
  241.     }
  242. }
  243.  
  244. void
  245. OpenBlankerWindow(void)
  246. {
  247.     struct Gadget *Ptr;
  248.     UWORD Index;
  249.     static char Title[80];
  250.  
  251.     if (BlankerWindow == NULL) {
  252.         strcpy(Title, "RCurves = <");
  253.         strcat(Title, PopKey);
  254.         strcat(Title, ">");
  255.  
  256.         if (BlankerWindow = OpenWindowTags(&NewBlankerWindow, WA_Title, Title,
  257.                                    WA_AutoAdjust, TRUE, WA_InnerWidth,
  258.                                 212, WA_InnerHeight, 118, TAG_DONE)) {
  259.  
  260.             AddTool(BlankerWindow, CloseWindow, NULL, "Could not open the window.");
  261.  
  262.             if ((BlankerVisualInfo = GetVisualInfo(BlankerWindow->WScreen, TAG_DONE))
  263.                 == NULL) {
  264.                 RemTool(BlankerWindow);
  265.                 return;
  266.             }
  267.             AddTool(BlankerVisualInfo, FreeVisualInfo, 0L,
  268.                     "Couldn't get visual data.");
  269.  
  270.             BlankerGadgets = NULL;
  271.             if ((Ptr = CreateContext(&BlankerGadgets)) == NULL) {
  272.                 RemTool(BlankerVisualInfo);
  273.                 RemTool(BlankerWindow);
  274.                 return;
  275.             }
  276.             AddTool(Ptr, FreeGadgets, 0L, "Couldn't allocate the gadgets.");
  277.  
  278.             /*
  279.              * Here we re-load the gadgets with the current settings. 
  280.              */
  281.             TimeGadgetTags[0].ti_Data = (ULONG) TimeOut;
  282.             ClientGadgetTags[0].ti_Data = (ULONG) ClientTimeOut;
  283.             SpeedGadgetTags[0].ti_Data = (ULONG) Speed;
  284.             SpeedGadgetTags[3].ti_Data = (ULONG) "%2ld";
  285.             LinesGadgetTags[0].ti_Data = (ULONG) NumLines;
  286.             LinesGadgetTags[3].ti_Data = (ULONG) "%3ld";
  287.             VertexGadgetTags[0].ti_Data = (ULONG) Vertices;
  288.             VertexGadgetTags[3].ti_Data = (ULONG) "%3ld";
  289.  
  290.             for (Index = 0L; Index < NUM_GADS; Index++) {
  291.                 NewBlankerGadgets[Index].ng_TopEdge += BlankerWindow->BorderTop;
  292.  
  293.                 NewBlankerGadgets[Index].ng_VisualInfo = BlankerVisualInfo;
  294.                 Ptr = CreateGadgetA((ULONG) BlankerGadgetKinds[Index], Ptr,
  295.                                     &NewBlankerGadgets[Index],
  296.                                     BlankerGadgetTagLists[Index]);
  297.                 if (Ptr == NULL) {
  298.                     CloseBlankerWindow();
  299.                     return;
  300.                 }
  301.                 NewBlankerGadgets[Index].ng_TopEdge -= BlankerWindow->BorderTop;
  302.             }
  303.  
  304.             AddGList(BlankerWindow, BlankerGadgets, 0L, -1L, NULL);
  305.             RefreshGadgets(BlankerGadgets, BlankerWindow, NULL);
  306.             GT_RefreshWindow(BlankerWindow, NULL);
  307.         }
  308.     }
  309.     ScreenToFront(BlankerWindow->WScreen);
  310.     WindowToFront(BlankerWindow);
  311.     ActivateWindow(BlankerWindow);
  312. }
  313.  
  314. /*
  315.  * Functions for Creating/Drawing/Removing the Lines
  316.  */
  317.  
  318. WORD __inline
  319. Insure1(WORD val)
  320. {
  321.     return (WORD)((val == 0) ? (WORD)1 : val);
  322. }
  323.  
  324. double __inline
  325. fInsure1(double val)
  326. {
  327.     return ((val < 1.0) ? 1.0 : val);
  328. }
  329.  
  330. /*
  331.  * Create the list of points.
  332.  */
  333. void *__regargs
  334. CreateLines(struct RetinaScreen *Screen,
  335.             LONG NumLines, LONG Speed)
  336. {
  337.     int i;
  338.  
  339.     if (!Screen)
  340.         return NULL;
  341.  
  342.     for (i = 0; i < LINE_BUF; i++) {
  343.         plist.point_list[i][0] = -1;
  344.         plist.point_list[i][1] = -1;
  345.         plist.point_list[i][2] = -1;
  346.         plist.point_list[i][3] = -1;
  347.     }
  348.  
  349.     plist.color = 1;
  350.     plist.max_points = NumLines;
  351.     plist.speed = Speed;
  352.     plist.last_point = 0;
  353.     plist.cur_point = NumLines;
  354.     plist.verts = (double)Vertices;
  355.     plist.point_list[NumLines][0] = drand48() * 4.0;
  356.     plist.point_list[NumLines][1] = drand48() * 4.0;
  357.     plist.point_list[NumLines][2] = drand48() * (double)Screen->rs_Width;
  358.     plist.point_list[NumLines][3] = drand48() * (double)Screen->rs_Height;
  359.     plist.av = drand48() + 0.01;
  360.     plist.bv = drand48() + 0.01;
  361.     plist.cv = fInsure1(drand48() * DELTA_V);
  362.     plist.dv = fInsure1(drand48() * DELTA_V);
  363.  
  364.     return (void *)&plist;
  365. }
  366.  
  367. int __inline __regargs
  368. fBound(double l, double *m, double u)
  369. {
  370.     if (*m < l) {
  371.         *m = l;
  372.         return 1;
  373.     }
  374.     if (*m >= u) {
  375.         *m = u - 1;
  376.         return 1;
  377.     }
  378.     return 0;
  379. }
  380.  
  381. int __inline __regargs
  382. Bound(int l, int *m, int u)
  383. {
  384.     if (*m < l) {
  385.         *m = l;
  386.         return 1;
  387.     }
  388.     if (*m >= u) {
  389.         *m = u - 1;
  390.         return 1;
  391.     }
  392.     return 0;
  393. }
  394.  
  395. void __inline
  396. Draw_Image(struct RetinaScreen *rs, double a, double b, double c, double d, double v)
  397. {
  398.     double f, x, y, z, xinc, yinc, finc, xmid, ymid;
  399.     int xi, yi, xp, yp;
  400.  
  401.     xp = -1;
  402.     yp = -1;
  403.  
  404.     xinc = (double)rs->rs_Width / 8.0;
  405.     yinc = (double)rs->rs_Height / 6.0;
  406.     xmid = (double)c;
  407.     ymid = (double)d;
  408.  
  409.     finc = PI2 / v;
  410.  
  411.     for (f = 0; f < PI2; f += finc) {
  412.         z = a * sin(f);
  413.  
  414.         x = z * cos(f);
  415.         y = z * sin(f);
  416.  
  417.         xi = (int)(x * xinc + xmid);
  418.         yi = (int)(y * yinc + ymid);
  419.  
  420.         Bound(0, &xi, rs->rs_Width);
  421.         Bound(0, &yi, rs->rs_Height);
  422.  
  423.         if (xp < 0) {
  424.             Retina_Line(rs, xi, yi, xi, yi);
  425.         } else {
  426.             Retina_Line(rs, xi, yi, xp, yp);
  427.         }
  428.         xp = xi;
  429.         yp = yi;
  430.  
  431.     }
  432.  
  433.     xi = (int)xmid;
  434.     yi = (int)ymid;
  435.  
  436.     Bound(0, &xi, rs->rs_Width);
  437.     Bound(0, &yi, rs->rs_Height);
  438.  
  439.     if (xp < 0) {
  440.         Retina_Line(rs, xi, yi, xi, yi);
  441.     } else {
  442.         Retina_Line(rs, xi, yi, xp, yp);
  443.     }
  444.  
  445. }
  446.  
  447. /*
  448.  * move the Lines and redraw them 
  449.  */
  450. void __regargs
  451. DrawLines(void *xplist, struct RetinaScreen *rs)
  452. {
  453.     double *pl;
  454.     PList *plist = xplist;
  455.  
  456.     /*
  457.      * If we don't have a screen, don't draw on it! (duh.) 
  458.      */
  459.     if (!rs)
  460.         return;
  461.  
  462.     if (plist->tic) {
  463.         plist->tic--;
  464.         return;
  465.     }
  466.     plist->tic = Insure1(MAX_SPEED - plist->speed);
  467.  
  468.     pl = plist->point_list[plist->cur_point];
  469.  
  470.     Retina_SetAPen(rs, plist->color);
  471.     plist->color++;
  472.  
  473.     Draw_Image(rs, pl[0], pl[1], pl[2], pl[3], plist->verts);
  474.  
  475.     plist->cur_point = (plist->cur_point + 1) % LINE_BUF;
  476.  
  477.     plist->point_list[plist->cur_point][0] = pl[0] + plist->av;
  478.     plist->point_list[plist->cur_point][1] = pl[1] + plist->bv;
  479.     plist->point_list[plist->cur_point][2] = pl[2] + plist->cv;
  480.     plist->point_list[plist->cur_point][3] = pl[3] + plist->dv;
  481.  
  482.     if (fBound(0.0, &plist->point_list[plist->cur_point][0], 4.0)) {
  483.         if (plist->av > 0) {
  484.             plist->av = -drand48() / 16.0 - 0.01;
  485.         } else {
  486.             plist->av = drand48() / 16.0 + 0.01;
  487.         }
  488.     }
  489.     if (fBound(0.0, &plist->point_list[plist->cur_point][1], 4.0)) {
  490.         if (plist->bv > 0) {
  491.             plist->bv = -drand48() / 16.0 - 0.01;
  492.         } else {
  493.             plist->bv = drand48() / 16.0 + 0.01;
  494.         }
  495.     }
  496.     if (fBound(0.0, &plist->point_list[plist->cur_point][2], (double)rs->rs_Width)) {
  497.         if (plist->cv > 0) {
  498.             plist->cv = -fInsure1(drand48() * DELTA_V);
  499.         } else {
  500.             plist->cv = fInsure1(drand48() * DELTA_V);
  501.         }
  502.     }
  503.     if (fBound(0.0, &plist->point_list[plist->cur_point][3], (double)rs->rs_Height)) {
  504.         if (plist->dv > 0) {
  505.             plist->dv = -fInsure1(drand48() * DELTA_V);
  506.         } else {
  507.             plist->dv = fInsure1(drand48() * DELTA_V);
  508.         }
  509.     }
  510.     Retina_SetAPen(rs, 0);
  511.     pl = plist->point_list[plist->last_point];
  512.     if (pl[0] >= 0.0)
  513.         Draw_Image(rs, pl[0], pl[1], pl[2], pl[3], plist->verts);
  514.  
  515.     plist->last_point = (plist->last_point + 1) % LINE_BUF;
  516.  
  517. }
  518.  
  519. /*
  520.  * The Main Loop
  521.  */
  522.  
  523. void
  524. main(LONG argc, UBYTE * argv[])
  525. {
  526.     char **ToolTypes;
  527.     CxObj *Broker, *ObjectList, *Filter;
  528.     struct IntuiMessage *IntMsg;
  529.     CxMsg *BlankerCxMsg;
  530.     LONG ThisTimeOut, TimeUntilBlank, TimeUntilBlack;
  531.     struct RetinaScreen *BlankerScreen = NULL;
  532.     struct MsgPort *ClientPort = NULL;
  533.     int BlankFlag = 0;
  534.  
  535.     /*
  536.      * open our Libraries 
  537.      */
  538.     AddTool(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",
  539.                                                37L), CloseLibrary, 0L,
  540.             "Couldn't open the intuition.library.");
  541.     AddTool(IconBase = OpenLibrary("icon.library", 37L), CloseLibrary, 0L,
  542.             "Couldn't open the icon library.");
  543.     AddTool(CxBase = OpenLibrary("commodities.library", 37L), CloseLibrary, 0L,
  544.             "Couldn't open the commoidities library.");
  545.     AddTool(GadToolsBase = OpenLibrary("gadtools.library", 37L), CloseLibrary, 0L,
  546.             "Couldn't open the gadtools library.");
  547.     AddTool(RetinaBase = (struct _xy_RetinaBase *)OpenLibrary("retina.library", 7L),
  548.             CloseLibrary, 0L, "Couldn't open the Retina library.");
  549.  
  550.     /*
  551.      * get our Arguments 
  552.      */
  553.  
  554.     if (ToolTypes = ArgArrayInit(argc, argv))
  555.         AddTool(ToolTypes, ArgArrayDone, 0L, NULL);
  556.  
  557.     /*
  558.      * get some Signals 
  559.      */
  560.  
  561.     ServerProcess = SysBase->ThisTask;
  562.     if ((bsp_TimerSig = AllocSignal(-1L)) == -1)
  563.         Quit(10, "Could not allocate a signal.");
  564.     AddTool((void *)bsp_TimerSig, FreeSignal, 0L, NULL);
  565.     if ((bsp_InputSig = AllocSignal(-1L)) == -1)
  566.         Quit(10, "Could not allocate a signal.");
  567.     AddTool((void *)bsp_InputSig, FreeSignal, 0L, NULL);
  568.     if ((bsp_ClientSig = AllocSignal(-1L)) == -1)
  569.         Quit(10, "Could not allocate a signal.");
  570.     AddTool((void *)bsp_ClientSig, FreeSignal, 0L, NULL);
  571.  
  572.     /*
  573.      * initialize our Broker = install us as a Commodity 
  574.      */
  575.  
  576.     AddTool(CxPort = CreateMsgPort(), DeleteMsgPortSafely, 0L,
  577.             "Failed to create a port.");
  578.  
  579.     NewBroker.nb_Descr = &VersionString[6];
  580.     NewBroker.nb_Pri = ArgInt(ToolTypes, "CX_PRIORITY", DEF_CX_PRI);
  581.     NewBroker.nb_Port = CxPort;
  582.     AddTool(Broker = CxBroker(&NewBroker, NULL), DeleteCxObjAll, 0L,
  583.             NULL);
  584.  
  585.     /*
  586.      * get Time Out, Client Time Out and Display mode 
  587.      */
  588.  
  589.     TimeOut = ArgIntRange(ToolTypes, "TIMEOUT", 1L, DEF_TIMEOUT, MAX_TIMEOUT);
  590.     ClientTimeOut = ArgIntRange(ToolTypes, "CLIENTTIMEOUT", 1L, DEF_CLIENT_TIMEOUT,
  591.                                 MAX_CLIENT_TIMEOUT);
  592.  
  593.     /*
  594.      * get Parameters for Line Movement 
  595.      */
  596.  
  597.     NumLines = ArgIntRange(ToolTypes, "LINES", MIN_LINES, DEF_LINES, MAX_LINES);
  598.     Speed = ArgIntRange(ToolTypes, "SPEED", 1L, DEF_SPEED, MAX_SPEED);
  599.     Vertices = ArgIntRange(ToolTypes, "VERTICES", MIN_VERTEX, DEF_VERTEX, MAX_VERTEX);
  600.  
  601.     /*
  602.      * install our hot keys 
  603.      */
  604.  
  605.     PopKey = ArgString(ToolTypes, "CX_POPKEY", DEF_POPKEY);
  606.     BlankKey = ArgString(ToolTypes, "BLANKKEY", DEF_BLANKKEY);
  607.  
  608.     if ((Filter = HotKey(PopKey, CxPort, HOTKEY_OPEN_WINDOW)) == NULL)
  609.         Quit(10, "The CX_POPKEY tool type is invalid.");
  610.     else
  611.         AttachCxObj(Broker, Filter);
  612.     if (CxObjError(Filter))
  613.         Quit(10, "Could not link to the commodity list.");
  614.  
  615.     if ((Filter = HotKey(BlankKey, CxPort, HOTKEY_BLANK_SCREEN)) == NULL)
  616.         Quit(10, "The BLANKKEY tool type is invalid.");
  617.     else
  618.         AttachCxObj(Broker, Filter);
  619.     if (CxObjError(Filter))
  620.         Quit(10, "Could not link to the commodity list.");
  621.  
  622.     /*
  623.      * install our "InputHandler" 
  624.      */
  625.  
  626.     ObjectList = CxCustom(BlankerAction, 0L);
  627.     AttachCxObj(Broker, ObjectList);
  628.     if (CxObjError(ObjectList))
  629.         Quit(10, "Could not link to the commodity list.");
  630.  
  631.     (void)ActivateCxObj(Broker, TRUE);
  632.     AddTool(Broker, ActivateCxObj, 0L, "The broker broke.");
  633.  
  634.     /*
  635.      * open Window on startup if not forbidden 
  636.      */
  637.  
  638.     if (stricmp(ArgString(ToolTypes, "CX_POPUP", ""), "NO"))
  639.         OpenBlankerWindow();
  640.  
  641.     /*
  642.      * increase our Priority 
  643.      */
  644.  
  645.     AddTool(FindTask(NULL), SetTaskPri, (LONG) SetTaskPri(FindTask(NULL), SERVER_PRI),
  646.             "Findtask failed!");
  647.  
  648.     /*
  649.      * start the Loop 
  650.      */
  651.  
  652.     TimeUntilBlank = ThisTimeOut = 10L * TimeOut;
  653.     TimeUntilBlack = 10L * ClientTimeOut;
  654.  
  655.     FOREVER
  656.     {
  657.         ULONG Mask;
  658.  
  659.         if (BlankerWindow)
  660.             Mask = Wait(MASK(bsp_TimerSig) | MASK(bsp_InputSig) | MASK(bsp_ClientSig) |
  661.                         MASK(CxPort->mp_SigBit) |
  662.                         MASK(BlankerWindow->UserPort->mp_SigBit) |
  663.                         SIGBREAKF_CTRL_C);
  664.         else
  665.             Mask = Wait(MASK(bsp_TimerSig) | MASK(bsp_InputSig) | MASK(bsp_ClientSig) |
  666.                         MASK(CxPort->mp_SigBit) | SIGBREAKF_CTRL_C);
  667.  
  668.         /*
  669.          * process Window Events 
  670.          */
  671.  
  672.         while ((BlankerWindow != NULL) && (IntMsg =
  673.                                  GT_GetIMsg(BlankerWindow->UserPort)))
  674.             switch (IntMsg->Class) {
  675.                 struct Gadget *Clicked;
  676.                 UWORD Code;
  677.  
  678.             case IDCMP_CLOSEWINDOW:
  679.                 GT_ReplyIMsg(IntMsg);
  680.                 CloseBlankerWindow();
  681.                 break;
  682.             case IDCMP_REFRESHWINDOW:
  683.                 GT_BeginRefresh(BlankerWindow);
  684.                 GT_EndRefresh(BlankerWindow, TRUE);
  685.                 break;
  686.             case IDCMP_GADGETUP:
  687.                 Code = IntMsg->Code;
  688.                 Clicked = (struct Gadget *)IntMsg->IAddress;
  689.                 GT_ReplyIMsg(IntMsg);
  690.                 switch (Clicked->GadgetID) {
  691.                 case GID_HIDE:
  692.                     CloseBlankerWindow();
  693.                     break;
  694.                 case GID_QUIT:
  695.                     Quit(0, "Normal Termination.");
  696.                 case GID_BLANK:
  697.                     if (TimeUntilBlank)
  698.                         TimeUntilBlank = ThisTimeOut = 2L;
  699.                     break;
  700.                 case GID_TIMEOUT:
  701.                     if (GetNum(BlankerWindow, Clicked, 1L, &TimeOut, MAX_TIMEOUT))
  702.                         TimeUntilBlank = ThisTimeOut = 10L * TimeOut;
  703.                     break;
  704.                 case GID_CLIENT:
  705.                     if (GetNum(BlankerWindow, Clicked, 1L, &ClientTimeOut, MAX_CLIENT_TIMEOUT))
  706.                         TimeUntilBlack = 10L * ClientTimeOut;
  707.                     break;
  708.                 case GID_LINES:
  709.                     NumLines = Code;
  710.                     break;
  711.                 case GID_VERTEX:
  712.                     Vertices = Code;
  713.                     break;
  714.                 case GID_SPEED:
  715.                     Speed = Code;
  716.                     break;
  717.                 }
  718.                 break;
  719.             case IDCMP_VANILLAKEY:
  720.                 Code = IntMsg->Code;
  721.                 GT_ReplyIMsg(IntMsg);
  722.                 switch ((char)Code) {
  723.                 case 'H':
  724.                 case 'h':
  725.                     CloseBlankerWindow();
  726.                     break;
  727.                 case 'Q':
  728.                 case 'q':
  729.                     Quit(0, "Normal Termination.");
  730.                 case 'B':
  731.                 case 'b':
  732.                     if (TimeUntilBlank)
  733.                         TimeUntilBlank = ThisTimeOut = 2L;
  734.                 }
  735.                 break;
  736.             default:
  737.                 GT_ReplyIMsg(IntMsg);
  738.             }
  739.  
  740.         /*
  741.          * process Commodity Messages 
  742.          */
  743.  
  744.         while (BlankerCxMsg = (CxMsg *) GetMsg(CxPort))
  745.             HandleCxMsg(Broker, BlankerCxMsg, &TimeUntilBlank, &ThisTimeOut);
  746.  
  747.         /*
  748.          * check for <CTRL>-C 
  749.          */
  750.  
  751.         if (Mask & SIGBREAKF_CTRL_C)
  752.             Quit(0, "Normal Termination.");
  753.  
  754.         /*
  755.          * Input detected, unblank if necessary 
  756.          */
  757.  
  758.         if (Mask & MASK(bsp_InputSig)) {
  759.             if (BlankFlag) {
  760.                 if (ClientPort)
  761.                     RemTool(ClientPort);
  762.  
  763.                 if (BlankerScreen) {
  764.                     SpritesOn(BlankerScreen);
  765.                     RemTool(BlankerScreen);
  766.                 } else
  767.                     Retina_DisplayOn();
  768.                 BlankFlag = 0;
  769.                 ThisTimeOut = 10L * TimeOut;
  770.             }
  771.             TimeUntilBlank = ThisTimeOut;
  772.         }
  773.         /*
  774.          * client has confirmed that it is still alive 
  775.          */
  776.  
  777.         if (Mask & MASK(bsp_ClientSig)) {
  778.             if (BlankerScreen)
  779.                 Retina_DisplayOn();
  780.             TimeUntilBlack = 10L * ClientTimeOut;
  781.             BlankFlag = 1;
  782.         }
  783.         /*
  784.          * 1/10 sec is over 
  785.          */
  786.  
  787.         if (Mask & MASK(bsp_TimerSig))
  788.             if (TimeUntilBlank) {
  789.                 TimeUntilBlank--;
  790.                 if (TimeUntilBlank == 0L) {        /*
  791.                                                  * Time Out reached,
  792.                                                  * blank * the screen 
  793.                                                  */
  794.                     struct ClientMessage ClientMessage;
  795.  
  796.                     BlankFlag = 1;
  797.                     BlankerScreen = CreateScreen(color_table);
  798.                     if (BlankerScreen)
  799.                         AddTool(BlankerScreen, DestroyScreen, 0L, NULL);
  800.                     else
  801.                         /*
  802.                          * do this if we're completely out of video
  803.                          * memory. 
  804.                          */
  805.                         Retina_DisplayOff();
  806.  
  807.                     ClientMessage.bcm_Screen = BlankerScreen;
  808.                     ClientMessage.bcm_SigMask = 1L << bsp_ClientSig;
  809.                     ClientMessage.bcm_Lines = NumLines;
  810.                     ClientMessage.bcm_Speed = Speed;
  811.                     ClientMessage.bcm_Vertices = Vertices;
  812.  
  813.                     if (ClientPort = CreateBlankerClient(RLinesClientProcess,
  814.                                                     &ClientMessage)) {
  815.                         TimeUntilBlack = 10L * ClientTimeOut;
  816.                         /*
  817.                          * try to start Client 
  818.                          */
  819.                         AddTool(ClientPort, DeleteBlankerClient, 0L, NULL);
  820.                     }
  821.                 }
  822.             } else {
  823.                 if ((BlankerScreen) && (RetinaBase->rb_FirstScreen != BlankerScreen)) {
  824.                     Retina_ScreenToFront(BlankerScreen);
  825.                     SpritesOff(BlankerScreen);
  826.                 }
  827.                 if (TimeUntilBlack) {
  828.                     TimeUntilBlack--;
  829.                     if (TimeUntilBlack == 0L)
  830.                         Retina_DisplayOff();    /*
  831.                                                  * Client Time Out *
  832.                                                  * reached, turn entire
  833.                                                  * * screen black 
  834.                                                  */
  835.                 }
  836.             }
  837.     }
  838. }
  839.